<template>
  <!-- 添加或修改订单产品工序分配任务对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本1信息" name="first">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-col :span="12" style="margin-top: 30px;">
                <el-form-item label="请选择工人">
                  <el-cascader :options="options" :props="props" :placeholder="'请选择工人'" v-model="regionValue"
                    @change="handleChangeRegion" show-all-levels clearable></el-cascader>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="转派数量" prop="count" required>
                <el-input-number v-model="form.count" :max="remainCount" @change="feipeiCount" placeholder="请输入转派数量" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="计划完成时间" prop="planEndTime">
                <el-date-picker clearable v-model="form.planEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择计划完成时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="单价" prop="money">
                <el-input v-model="form.money" placeholder="请输入单价" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24" style="margin-top: 30px;">
            <el-col :span="12">
              <el-form-item label="总价" prop="allMoney">
                <el-input v-model="form.allMoney" placeholder="请输入总价" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <!--<el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading">
          <el-table-column align="center" label="序号" type="index" width="50">
            <template slot-scope="scope">
              <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
            </template>
</el-table-column>
<el-table-column label="订单编号" align="center" prop="projectNo" />
&lt;!&ndash; <el-table-column label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip />&ndash;&gt;
<el-table-column label="产品编码" align="center" prop="goodsCode" />
<el-table-column align="center" label="产品名称" prop="goodsName" />
<el-table-column align="center" label="产品型号" prop="goodModel" />
<el-table-column label="工艺名称" align="center" prop="gongyiName" />
<el-table-column label="工序编码" align="center" prop="code" />
<el-table-column label="工序名称" align="center" prop="name" />

<el-table-column label="总数量" align="center" prop="counts" />
<el-table-column label="已分配数量" align="center" prop="fenpeiCount" />
&lt;!&ndash; <el-table-column label="剩余分配数量" align="center" prop="remainCount" />&ndash;&gt;
<el-table-column label="剩余分配数量" width="210">
  <template slot-scope="scope">
              <el-input-number v-model="scope.row.remainCount" :precision="2" :min="0" ></el-input-number>
            </template>
</el-table-column>
<el-table-column label="已完成数量" align="center" prop="finishCount" />
<el-table-column label="剩余完成数量" align="center" prop="remainfinishCount" />
<el-table-column align="center" label="备注" prop="remark" />
</el-table>-->
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定 转 派</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAction, getById, updateAction ,getAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "ZhuanpaiForm",
  dicts: ['sys_user'],
  data() {
    return {
      regionValue: [],
      props: {
        multiple: true,
        emitPath: false,
        value: 'id',
        label: 'label',
        children: 'children'
      },
      options: [],
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      parentId: null,
      counts: null,
      // 表单参数
      form: {},
      columns: {},
      remainCount: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {},
        id: null,
        code: null,
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "工人不能为空", trigger: "blur" }
        ],
        count: [
          { required: true, message: "转派数量不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value == 0) {
                callback(new Error('转派数量不能为0'));
              } else {
                callback();
              }
            }, trigger: 'blur'
          }
        ],
      },
      url: {
        getById: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/',
        addUserTaskZhuanpai: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/addUserTaskZhuanpai',
        edit: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/edit',
        getUserByDept: '/system/user/getUserByDept',
        getAllShpper: '/system/dept/treeselect',
        getConsumer: '/system/user/getUserByDept',
        userTreeSelect: '/system/dept/userTreeSelect'
      },
    };
  },
  created() {
    this.getUser();
  },
  methods: {
     handleChangeRegion(e) {
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getUser() {
      let that = this;
      getAction(that.url.userTreeSelect, { parentId: '57d779853f0f4d14b03d51db30099456' }).then(res => {
        that.options = res.data;
        that.options.map((item) => {
          item = {
            value: item.id,
            label: item.label,
            children: item.children ? item.children.map(child => ({
              value: child.id,
              label: child.label
            })) : []
          }
        });
      })
    },
    selectUser(value) {
      let that = this;
      that.$nextTick(() => {
        let arr = that.$refs.myselectUser;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].currentValue == value) {
            that.form.userName = arr[i].label;
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xuhao: null,
        code: null,
        name: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        shuoming: null,
        status: "0",
        projectId: null,
        projectCode: null,
        projectName: null,
        saleId: null,
        goodsId: null,
        gongyiId: null,
        gongxuId: null,
        gongyiCode: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        goodsBrand: null,
        doStartTime: null,
        doEndTime: null,
        planStartTime: null,
        planEndTime: null,
        userId: null,
        userName: null,
        money: null,
        count: null,
        allMoney: null,
        remainCount: null,
        parentId: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    zhuanpaigShow(selectRows) {
      this.reset();
      this.remainCount = selectRows.remainCount;
      this.form.count = selectRows.remainCount;
      this.form.money = selectRows.money;
      this.form.planEndTime = selectRows.planEndTime;
      this.form.allMoney = selectRows.allMoney;
      this.form.id = selectRows.id;
      //this.columns=selectRows;
      this.open = true;
      this.loading = false;
      this.title = "转派订单产品工序任务";
    },
    feipeiCount() {
      let that = this;
      if (that.form.money && that.form.count) {
        let totlePrice = this.functionNumberMul(that.form.count, that.form.money);
        that.$set(that.form, "allMoney", totlePrice);
      }
    },
    functionNumberMul(arg1, arg2) {
      var m = 0;
      var s1 = arg1.toString();
      var s2 = arg2.toString();
      try {
        m += s1.split(".")[1].length;
      } catch (e) {
      }
      try {
        m += s2.split(".")[1].length;
      } catch (e) {
      }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单产品工序分配任务";
      });
    },
    /** 提交按钮 */
    submitForm() {

      let that = this;
      let userIdres = '';
      userIdres = that.regionValue.join(',');
      /*if (userIdres.length > 1) {
        userIdres = userIdres.slice(0, -1);
      }*/
      if (userIdres == '') {
        that.$modal.msgError("请选择工人");
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.userIds = userIdres;
          alert(userIdres+"!!!");
          addAction(this.url.addUserTaskZhuanpai, this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.$emit('ok');
          });
        }
      });
    },
  }
};
</script>
